package demo;

import util.DBUtil;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

/**
 * 演示如何调用存储过程
 */
public class CallProcedureDemo {
    /**
     * 调用不带参的调用过程
     */
    public static void demo1() throws Exception {
        try (Connection conn = DBUtil.getConnection()) {
            /*
            调用存储过程必须使用CallableStatement
            CallableStatement是PreparedStatement的子接口
             */
            CallableStatement cs = conn.prepareCall("{call pro_find()}");
            cs.setInt(1, 3);

            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt("id") + ""
                        + rs.getString("name")
                        + "" + rs.getInt("salary")
                        + "" + rs.getInt("age"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;

        }
    }

    public static void main(String[] args) throws Exception {
        demo1();
    }
}
